package cn.fl.tag.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.cmm.base.framework.context.ApplicationContextAccessor;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.cmm.cache.framework.utils.PropertiesCacheUtils;
import cn.fl.flow.service.IFlowFrmAthRecService;
import cn.fl.framework.base.context.CurrentThreadContext;
import fl.constant.facade.framework.constant.DictionaryConstant;
import fl.flow.facade.flow.condition.FlowFrmAthRecQC;

/**
 * 流程表单权限TAG
 * @author FUJINGRUN
 */
public class FlowFrmAthTag extends TagSupport{

	private static final long serialVersionUID = -6364875959608952373L;

	private static final Logger logger = LoggerFactory.getLogger(FlowFrmAthTag.class);
	
	/**业务主键 */
	private String dataId;
	/**流程实例ID*/
	private String pInstId;
	/**流程上一个节点任务ID,多个已逗号分割*/
	private String preTaskIds;
	/**系统代码*/
	private String sysCd;
	/**要对比的代码*/
	private String athCd;
	
	/**
	 * 系统资源服务
	 */
	private IFlowFrmAthRecService flowFrmAthRecService = (IFlowFrmAthRecService)ApplicationContextAccessor.getBean("flowFrmAthRecServiceImpl");
	
	@Override
	public int doStartTag() throws JspException {
		try {
			FlowFrmAthRecQC qc = new FlowFrmAthRecQC();
			/*if(StringUtils.isEmpty(dataId)){
				BaseLogUtils.info(logger, "doStartTag", "未获取到dataId!");
				return EVAL_BODY_INCLUDE;
			}*/
			
			if(StringUtils.isEmpty(pInstId)){
				BaseLogUtils.info(logger, "doStartTag", "未获取到pInstId!");
				return EVAL_BODY_INCLUDE;
			}
			
			if(StringUtils.isEmpty(preTaskIds)){
				BaseLogUtils.info(logger, "doStartTag", "未获取到preTaskIds!");
				return EVAL_BODY_INCLUDE;
			}
			
			if(StringUtils.isNotEmpty(dataId)){
				qc.setDataId(Long.valueOf(dataId));
			}
			qc.setProcInstId(pInstId);
			if(StringUtils.isEmpty(sysCd)){
				qc.setSysCd(CurrentThreadContext.getFlServiceSysCd());
			}else{
				qc.setSysCd(sysCd);
			}
			
			qc.setTaskIdList(this.getPreTaskIdList());
			Map<String, String> map = this.flowFrmAthRecService.searchFlowFrmAthRecToMap(qc);
			String AE = PropertiesCacheUtils.getProperty(DictionaryConstant.CODE_FLW_FRM_ATH_AE_CD);
			//如果返回值有指定的全部修改, 则全部返回可用
			if(map.containsKey(AE)){
				return EVAL_BODY_INCLUDE;
			}
			if(map.containsKey(athCd)){//如果存在说明是禁用,这里的逻辑是非正常思维逻辑,是反向逻辑,主要是为了解决在流程中同一个页面在不同地方被引用权限
				return SKIP_BODY;
			}else{
				return EVAL_BODY_INCLUDE;
			}
		} catch (Exception e) {
			return EVAL_BODY_INCLUDE;//如果出错默认就是可以使用
		}
	}
	
	
	/**业务主键 */
	public String getDataId() {
		return dataId;
	}
	/**业务主键 */
	public void setDataId(String dataId) {
		this.dataId = dataId;
	}
	/**流程实例ID*/
	public String getpInstId() {
		return pInstId;
	}
	/**流程实例ID*/
	public void setpInstId(String pInstId) {
		this.pInstId = pInstId;
	}
	/**流程上一个节点任务ID,多个已逗号分割*/
	public String getPreTaskIds() {
		return this.preTaskIds;
	}
	/**流程上一个节点任务ID,多个已逗号分割*/
	public void setPreTaskIds(String preTaskIds) {
		this.preTaskIds = preTaskIds;
	}
	/**系统代码*/
	public String getSysCd() {
		return sysCd;
	}
	/**系统代码*/
	public void setSysCd(String sysCd) {
		this.sysCd = sysCd;
	}
	/**要对比的代码*/
	public String getAthCd() {
		return athCd;
	}
	/**要对比的代码*/
	public void setAthCd(String athCd) {
		this.athCd = athCd;
	}
	
	/**流程上一个节点任务ID,多个已逗号分割*/
	private List<String> getPreTaskIdList() {
		List<String> tkIds = new ArrayList<>();
		String tk = this.preTaskIds;
		if(StringUtils.isEmpty(tk)){
			return tkIds;
		}
		String [] tkArray = tk.split(",");
		for (String cd : tkArray) {
			if(tkIds.contains(cd))continue;
			tkIds.add(cd);
		}
		return tkIds;
	}
}
